#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "DuLink.h"
#include "Stack.h"
int InitDL(DuLinkList *L)
{
    *L = (DuLinkList)malloc(sizeof(DuLinkNode));
    if (!*L)
    {
        return 0;
    }
    (*L)->next = NULL;
    (*L)->prior = NULL;
    (*L)->info = NULL; 
    return 1;
}

void Insert_DL(DuLinkList *L, int position, Elemtype str)
{
  
    
    int i = 0;
    DuLinkList p = *L;
    
    while (p && i < position - 1)
    {
        i++;
        p = p->next;
    }
    
    
    if (!p) 
    {
        printf("Position out of range!\n");
        return;
    }
    
    DuLinkList q = (DuLinkList)malloc(sizeof(DuLinkNode));
    if (str != NULL) 
    {
        q->info = (char*)malloc(strlen(str) + 1);  
        strcpy(q->info, str);
    } 
    else 
    {
        q->info = NULL;
    }
    
    q->next = p->next;
    q->prior = p;
    
    if (p->next) 
    {
        p->next->prior = q;
    }
    p->next = q;
    
    printf("Inserted at position %d\n", position);
}

Elemtype Delet_DL(DuLinkList *L, int position)
{
    if (position < 1) 
    {
        printf("Invalid position!\n");
        return NULL;
    }
   
    
    int i = 0;
    DuLinkList p = (*L)->next;        
    
    while (p && i < position - 1)
    {
        i++;
        p = p->next;
    }
    
    if (!p) 
    {
        printf("Position out of range!\n");
        return NULL;
    }
    
    Elemtype str = p->info;
    Elemtype str_copy = NULL;
    if (str != NULL) 
    {
        str_copy = (Elemtype)malloc(strlen(str) + 1);
        strcpy(str_copy, str);
    }
    if (p->prior) 
       p->prior->next = p->next;
    if (p->next) 
       p->next->prior = p->prior;
    if(p->info)
        free(p->info);
    free(p);
    printf("Deleted from position %d: %s\n", position, str_copy);
    return str_copy;
}
void Print_DL(DuLinkList L)
{
    if (!L || !L->next) 
    {
        printf("The list is empty!\n");
        return;
    }
    
    DuLinkList p = L->next; 
    int i = 1;
    
    printf("****** Current List ******\n");
    while(p)
    {
        printf("%d. %s\n", i, p->info);
        p = p->next;
        i++;
    }
    printf("**************************\n");
}
Elemtype Search_Line(DuLinkList L, int line)
{
    if (line < 1) 
    {
        printf("Invalid line number!\n");
        return NULL;
    }
    
    if (!L || !L->next) 
    {
        printf("The list is empty!\n");
        return NULL;
    }
    
    DuLinkList p = L->next;
    int i = 1;
    
    while (p != NULL && i < line)
    {
        i++;
        p = p->next;
    }
    
    if (p == NULL) 
    {
        printf("Line %d out of range!\n", line);
        return NULL;
    }
    
    return p->info;
}
void Destroy_DL(DuLinkList *L)
{
    if (!L || !*L) return;
    
    DuLinkList p = (*L)->next;
    while (p)
    {
        DuLinkList temp = p;
        p = p->next;
        if (temp->info)
            free(temp->info);
        free(temp);
    }
    
    free(*L);
    *L = NULL;
}